package com.zmz.Stream;


import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Create with IntelliT IDEA
 *
 * @Author: zhengmingzhe
 * @Date: 2022/07/31/0:36
 * @Description: 获取Stream流
 */
public class Stream02 {
    public static void main(String[] args) {
        //1.Collection.stream()方法,用集合创建
        List<String> list = Arrays.asList("1", "2", "3");
        Stream<String> stream0 = list.stream();

        //2.使用java.util.Arrays.stream(T[] array)方法用数组创建
        String[] arr = {"a", "b", "c", "d"};
        Stream stream1 = Arrays.stream(arr);
        Stream stream2 = Arrays.stream(arr).sequential();  //顺序流
        Stream stream3 = Arrays.stream(arr).distinct();   //去重流


        //3.使用Stream的静态方法： of(),   iterate(),  generate()
        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
        System.out.println(integerStream);
//        System.out.println(integerStream.max());

        //获取数组中的最大值
        int[] integerArr = {1, 2, 3, 4, 5};
        IntStream stream4 = Arrays.stream(integerArr);
        System.out.println(Arrays.stream(integerArr).max().getAsInt());


        Stream limit = Stream.iterate(0, x -> x + 3).limit(3); //输出0 3  6  迭代功能,第一个参数是起始值.limit限制次数
        limit.forEach(t -> System.out.println(t));

        AtomicInteger integer = new AtomicInteger(10); //创建一个原子类 从10开始 每次自增1  自增2次
        Stream<Integer> limit1 = Stream.generate(() -> integer.incrementAndGet()).limit(2);
        limit1.forEach(System.out::println);

    }
}
